package com.ifonly.activerecord;

import com.ifonly.activerecord.callback.ModelCallback;
import com.ifonly.aop.Enhancer;

import java.util.HashMap;
import java.util.Map;

/**
 * @author ifonly
 * @version 1.0 2016-01-13 11:09
 * @since JDK 1.6
 */
public class ModelMapper {

    private Map<Class<? extends Model>, Object> modelMapping = new HashMap<Class<? extends Model>, Object>();

    public void addModelMapper(Class<? extends Model> clazz) {
        if (modelMapping.get(clazz) != null) {
            throw new IllegalArgumentException("Model Mapping for Class[" + clazz.getName() + "] exist");
        }
        Model model = Enhancer.enhance(clazz, new ModelCallback());
        modelMapping.put(clazz, model);
    }

    @SuppressWarnings("unchecked")
    public <T> T getModel(Class<? extends Model> clazz) {
        return (T) modelMapping.get(clazz);
    }

}
